#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;

int isKeyword(char* t, int n)
{
    int i;
    if(n < 2 || n > 6)
        return 0;
    for(i = 0; i < 6; ++ i)
    {
        if (strcmp(t, keyword[i]) == 0)
            return i+1;
    }
    if (i == 6)
        return 0;
}
void lexical(FILE *fp) {
    char x = getc(fp);
	int i;
	while(x == ' ' || x == '\t' || x == '\n')
        x = getc(fp);
	char temp[100];
    for(i = 0; x != ' ' || x != '\t' || x != '\n';)
    {
        if (isdigit(x))
        {
            temp[i] = x;
            if (i == 0)
                token = 11;
            ++ i;
        }
        else if (isalpha(x))
        {
            if (token == 11)
            {
                token = -1;
                break;
            }
            temp[i] = x;
            ++ i;
            token = 10;
        }
        else
        {
            if(i != 0 )
            {
                ungetc(x, fp);
                break;
            }
            switch(x){
            case '#':
                token = 0;
                temp[0] = x;
                ++ i;
                break;
            case '+':
                token = 13;
                temp[0] = x;
                ++ i;
                break;
            case '-':
                token = 14;
                temp[0] = x;
                ++ i;
                break;
            case '/':
                token = 15;
                temp[0] = x;
                ++ i;
                break;
            case ':':
                temp[0] = x;
                ++ i;
                x = fgetc(fp);
                if(x == '=')
                {
                    temp[1] = x;
                    token = 18;
                    i = 2;
                }
                else
                {
                    ungetc(x, fp);
                    token = 17;
                    i = 2;
                }
                break;
            case '<':
                temp[0] = x;
                ++ i;
                x = fgetc(fp);
                if(x == '=')
                {
                    temp[1] = x;
                    token = 22;
                    i = 2;
                }
                else if (x == '>')
                {
                    temp[1] = x;
                    token = 21;
                    i = 2;
                }
                else
                {
                    ungetc(x, fp);
                    token = 20;
                    i = 2;
                }
                break;
            case '>':
                temp[0] = x;
                ++ i;
                x = fgetc(fp);
                if(x == '=')
                {
                    temp[1] = x;
                    token = 24;
                    i = 2;
                }
                else
                {
                    ungetc(x, fp);
                    token = 23;
                }
                break;
            case '=':
                temp[0] = x;
                ++ i;
                token = 25;
                break;
            case ';':
                temp[0] = x;
                ++ i;
                token = 26;
                break;
            case '(':
                temp[0] = x;
                ++ i;
                token = 27;
                break;
            case ')':
                temp[0] = x;
                ++ i;
                token = 28;
                break;
            default:
                break;
            }
            break;
        }
        x = getc(fp);
    }
    temp[i] = '\0';
    if (token == -1)
        return;
    if(token == 10)
    {
        if((token = isKeyword(temp, i+1)) == 0)
            token = 10;
    }
    if(token == 11)
        num = atoi(temp);
    strcpy(id,temp);
	//process x
	//process each regex here
	//store the result in global variables id, num, or token
	//in case of backtrack, use
	//ungetc(x, fp);

}

int main(int argc, const char *argv[])
{
	if(argc < 2) {
		printf("insufficient args\n");
		return 1;
	}
	FILE *fp = fopen(argv[1], "r");
	do {
		lexical(fp);
		switch(token) {
			case 10: printf("(id, %s)\n", id); break;
			case 11: printf("(num, %d)\n", num); break;
			case -1: printf("error!\n"); break;
			default: printf("(%d, %s)\n", token, id);
		}
	} while(token != 0);


	fclose(fp);
	return 0;
}
